<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/5/8
 * Time: 11:45
 */
namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Page;

/**
 * 问题api
 * Class Problem
 * @package app\api\controller
 */
class Problem extends Api{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 提问
     * @throws \think\Exception
     */
    public function add(){
        $content=$this->request->request('content')?trim($this->request->request('content')):$this->error('请输入问题！');

        //获得用户ip
        $ip=$this->request->ip();

        //根据ip是否存在用户信息
        $user=db('user')->where('joinip="'.$ip.'"')->field('id,status')->find();
        //用户不存在添加用户
        if(empty($user)){
            $defaultGroup=model('user_group')->where('status="normal" and name="默认组"')->field('id')->find();
            $userData['group_id']=$defaultGroup['id'];
            $userData['joinip']=$ip;
            $userData['jointime']=time();
            $userData['status']='normal';
            model('user')->allowField(true)->save($userData)?$user=db('user')->where('joinip="'.$ip.'"')->field('id,status')->find():$this->error('系统异常！');
        }
        if($user['status']=='hidden') $this->error('用户状态异常！');

        //当前用户是否有提交过这个问题
        db('problem')->where('user_id='.$user['id'].' and content="'.$content.'"')->count()?$this->error('您已提过相同问题！'):'';

        //添加问题
        $problemData['user_id']=$user['id'];
        $problemData['content']=$content;
        model('problem')->allowField(true)->save($problemData)?'':$this->error('系统异常！');

        $this->success('提交成功.');
    }

    public function getProblemList(){
        $keywords = $this->request->request('keywords');
        $num = $this->request->request('num');
        $currentPage=$this->request->request('page');

        if(empty($num) || empty($currentPage) || $num!=6 || !is_numeric($currentPage) || $currentPage<1 ){
            $this->error('非法请求');
        }

        $start=($currentPage-1)*$num;

        $where='1=1';
        if(!empty($keywords)){
            $where.=' and content like "%'.$keywords.'%"';
        }

        //所有问题
        $problemList=db('problem')->where($where)->order('id desc')->field('id,content,from_unixtime(createtime,"%Y-%m-%d") as createtime')->limit($start,$num)->select();
        foreach ($problemList as &$v){
            $v['answerCount']=db('answer')->where('problem_id='.$v['id'])->count();
            if(!empty($v['answerCount'])) {
                $v['answer']=db('answer')->where('problem_id='.$v['id'])->field('id,user_id,content,from_unixtime(createtime,"%Y-%m-%d") as createtime')->find();
                $v['answer']['user']=db('user')->where('id='.$v['answer']['user_id'])->field('id,nickname,avatar,joinip')->find();
                $v['answer']['commentCount']=db('comment')->where('answer_id='.$v['answer']['id'])->count();
            }
        }
        unset($v);
        $count=db('problem')->where($where)->count();

        //分页
        $page=new Page();
        $show=$page->create($count,$currentPage,6);

        $data['problemList']=$problemList;
        $data['show']=$show;
        $this->success('请求成功',$data);
    }

}